package com.ly.user_system.utils;

import com.ly.user_system.base.BaseUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class UserContextUtil {

    public static void setUserContext(Authentication authentication){
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }

    public static Long getUserId(){
        if (getUserContext().getPrincipal() instanceof BaseUserDetails details) {
            return details.getUserId();
        }
        return null;
    }

    public static Authentication getUserContext(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static void clear() {
        SecurityContextHolder.clearContext();
    }
}
